import pytest
from api.jiuchao_emp_api import JCEmpApi
from common.assert_tools import common_assert
from common.get_header import get_header
from common.py09_db_tools import DBTools
from config import TEL, BASE_DIR
from common.read_json_file import read_json_data


# 定义测试类，实现参数化
class TestAddEmpParams(object):
    header = None
    def setup_class(self):
        self.header = get_header()

    def setup(self):
        # 注释是因为 添加用户，用户名不能重复，我已经存在ceshi1111用户名了，所以我要删掉在参数化，也可以不用ceshi1111换一个用户名就不用写这个代码了
        # del_sql = "delete from system_users where username = 'ceshi1111';"
        # # 执行删除并打印日志，确认是否删除成功
        # print(f"setup 执行删除 SQL: {del_sql}")
        # DBTools.db_uid(del_sql)
        del_sql = f"delete from system_users where mobile = '{TEL}';"
        DBTools.db_uid(del_sql)

    def teardown(self):
        # del_sql = "delete from system_users where username = 'ceshi1111';"
        # # 执行删除并打印日志，确认是否删除成功
        # print(f"setup 执行删除 SQL: {del_sql}")
        # DBTools.db_uid(del_sql)

        del_sql = f"delete from system_users where mobile = '{TEL}';"
        DBTools.db_uid(del_sql)

    filename = BASE_DIR + "/data/add_emp_data.json"

    # 定义通用测试方法  -实现参数化
    @pytest.mark.parametrize("desc,json,status_code,code,msg", read_json_data(filename))
    def test_add_emp(self, desc, json, status_code, code, msg):
        req_header = self.header
        # 调用自己封装的api，发送请求
        res = JCEmpApi.add_emp(req_header, json)
        print(desc, ":", res.json())
        # 调用之前封装的断言
        common_assert(res, status_code, code, msg)

    # # 定义测试方法-添加成功所有参数
    # def test02_username_none(self):
    #     # 调用自己封装的api，发送请求
    #     req_header = {"content-type": "application/json", "Authorization": "Bearer 6d498fa9f75a4e448ef9c73db7be048d"}
    #     json = {"nickname": "测试899", "deptId": "", "mobile": TEL, "email": "", "username": None,
    #             "password": "123456", "postIds": [], "remark": "", "status": 0, "roleIds": []}
    #     res = JCEmpApi.add_emp(req_header, json)
    #     print("用户名为空:", res.json())
    #     # 调用之前封装的断言
    #     common_assert(res, 200, 400, "请求参数不正确:用户账号不能为空")
    #
    # # 定义测试方法-添加成功所有参数
    # def test03_pwd_none(self):
    #     # 调用自己封装的api，发送请求
    #     req_header = {"content-type": "application/json", "Authorization": "Bearer 6d498fa9f75a4e448ef9c73db7be048d"}
    #     json = {"nickname": "测试899", "deptId": "", "mobile": TEL, "email": "", "username": "ceshi1111",
    #             "password": None, "postIds": [], "remark": "", "status": 0, "roleIds": []}
    #     res = JCEmpApi.add_emp(req_header, json)
    #     print("密码为空:", res.json())
    #     # 调用之前封装的断言
    #     common_assert(res, 200, 400, "请求参数不正确:密码不能为空")
